#include "comm.hpp"

int main()
{
    // 创建公共的key值
    key_t key = ftok(PATHNAME, PROJ_ID);
    assert(key != -1);
    Log("create key done", Debug) << " server key : ";
    printf("%02X\n", key);

    // 创建共享的内存，由于server是通信的发起者，所以应当创建的是一个全新的
    int shmid = shmget(key, SHM_SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid == -1)
    {
        perror("shmget");
        exit(1);
    }
    Log("create shm done", Debug) << " : " << shmid << endl;
    sleep(10);

    // 将指定的共享内存挂接到继承的地址空间
    char *buffer = (char *)shmat(shmid, nullptr, 0);
    Log("attach shm done", Debug) << " : " << shmid << endl;
    sleep(10);

    // 将指定的共享内存，从自己的地址空间中去关联
    int a = shmdt(buffer);
    assert(a != -1);
    (void)a;
    Log("detach shm done", Debug) << " : " << shmid << endl;
    sleep(10);
    
    // 删除共享内存
    int n = shmctl(shmid, IPC_RMID, nullptr);
    assert(n != -1);
    (void)n;
    Log("delete shm done", Debug) << " : " << shmid << endl;
    return 0;
}